package fc.testmain;

import cn.hutool.http.HttpRequest;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;

import fc.FcHttpCon;
import fc.entity.OaEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class T100GONGDAN_CREATE {
    public static void main(String[] args) {


        String createdate="2024-08-15";
        String timestamp="20230912123204361";
        String creater="ITADMIN";

        OaEntity oe=new OaEntity();
        oe.setLJBH("DPJ-MWI-2024080001-01");
        oe.setSL("1");
        oe.setDW("PCS");


        oe.setWLXQSJ("2024-09-01");
        oe.setXMBH("ZH00-RD026");
        oe.setREM("20240815058(改善脉搏AOM后外光路损耗)");
        List<OaEntity> DT1=new ArrayList<OaEntity>();

        OaEntity dt=new OaEntity();
        dt.setLJBH("B0040102469Aaaa");
        dt.setSL("1");
        dt.setDW("PCS");
        dt.setREM("吴志峰");

        OaEntity dtt=new OaEntity();
        dtt.setLJBH("Z0402022A-100-D113A");
        dtt.setSL("1000");
        dtt.setDW("PCS");
        dtt.setREM("吴志峰");

        DT1.add(dt);
        //DT1.add(dtt);
        oe.setDT1(DT1);

        String testparam="";
        for(int i=0;i<oe.getDT1().size();i++){
            if (!"".equals(testparam)){
                testparam=testparam+",";
            }
            testparam+=
                    "{"+
                    "\"seq_no\": \""+(i+1)+"\","+
                    "\"part_no\": \" \","+
                    "\"op_no\": \" \","+
                    "\"op_seq\": \" \","+
                    "\"bom_item_no\": \""+oe.getDT1().get(i).getLJBH()+"\","+
                    "\"issue_item_no\": \""+oe.getDT1().get(i).getLJBH()+"\","+
                    "\"product_feature_no\": \" \","+
                    "\"mandatory_feature\": \"1\","+
                    "\"recoil_material\": \"0\","+
                    "\"allow_error_rate\": \"0\","+
                    "\"adjust_issuance_qty\": \"0\","+
                    "\"qty\": \""+oe.getDT1().get(i).getSL()+"\","+
                    "\"unit_no\": \""+oe.getDT1().get(i).getDW()+"\","+
                    "\"outsourcing_purchasing_qty\": \"0\","+
                    "\"designation_warehouse_no\": \"\","+
                    "\"designation_storage_spaces_no\": \"\","+
                    "\"designation_lot_no\": \"\","+
                    "\"designation_inventory_management_features\": \" \","+
                    "\"remark\": \""+oe.getDT1().get(i).getREM()+"\""+
                    "}";
        }

        String json_param=
        "{"+
                "\"key\": \"dccb6a7b66b7e1d13dbe68ed70084b7c\","+
                "\"type\": \"sync\","+
                "\"host\": {"+
                "\"prod\": \"OAPI\","+
                "\"ip\": \"192.168.0.30\","+
                "\"lang\": \"zh_CN\","+
                "\"acct\": \"tiptop\","+
                "\"timestamp\": \""+timestamp+"\""+//--时间戳要改
                "},"+
                "\"service\": {"+
                "\"prod\": \"T100\","+
                "\"name\": \"t100.oapi.wo.data.crt\","+
                "\"ip\": \"192.168.0.30\","+
                "\"id\": \"topprd\""+
                "},"+
                "\"datakey\": {"+
                "\"EntId\": \"2\","+
                "\"CompanyId\": \"ZH00\""+
                "},"+
                "\"payload\": {"+
                "\"std_data\": {"+
                "\"parameter\": {"+
                "\"enterprise_no\": \"23\","+
                "\"site_no\": \"ZH00\","+
                "\"is_digiwin_product\": \"Y\","+
                "\"wo_info\": ["+
                "{"+
                "\"source_no\": \"来源单号-orig-bill-test\","+
                "\"doc_slip\": \"YFNPJ\","+
                "\"doc_date\": \""+createdate+"\","+
                "\"production_control_no\": \""+creater+"\","+
                "\"wo_type\": \"4\","+
                "\"outsourcing_type\": \"1\","+
                "\"issue_rule\": \"1\","+
                "\"production_item_no\": \""+oe.getLJBH()+"\","+
                "\"production_item_feature\": \" \","+
                "\"production_qty\": \""+oe.getSL()+"\","+
                "\"production_unit_no\": \""+oe.getDW()+"\","+
                "\"process_no\": \"\","+
                "\"wo_source_type\": \"0\","+
                "\"wo_source\": \"\","+
                "\"wo_source_seq\": \"\","+
                "\"wo_source_term_seq\": \"\","+
                "\"plan_date_s\": \""+oe.getWLXQSJ()+"\","+
                "\"department_supplier_no\": \"\","+
                "\"cost_center_no\": \"10000159\","+
                "\"bom_version\": \"\","+
                "\"pbi_no\": \"\","+
                "\"urgency_rate\": \"\","+
                "\"priority_seq\": \"\","+
                "\"project_no\": \""+oe.getXMBH()+"\","+
                "\"workstation_id\": \"\","+
                "\"shift_no\": \"\","+
                "\"plan_stock_in_warehouse_no\": \"\","+
                "\"plan_stock_in_location_no\": \"\","+
                "\"plan_stock_in_lot_no\": \"\","+
                "\"plan_stock_in_inventory_management_features\": \" \","+
                "\"create_employee_no\": \""+creater+"\","+
                "\"create_date\": \""+createdate+"\","+
                "\"remark\": \""+oe.getREM()+"\","+
                "\"doc_approve_set\": \"N\","+
                "\"prepare_material_detail\": ["+

                testparam+

                "],"+
                "\"production_item_no_list\": ["+
                "{"+
                "\"production_type\": \"1\","+
                "\"item_no\": \""+oe.getLJBH()+"\","+
                "\"product_feature\": \" \","+
                "\"plan_output_qty\": \""+oe.getSL()+"\""+
                "}"+
                "]"+
                "}"+
                "]"+
                "}"+
                "}"+
                "}"+
                "}";

        //调用 ECOLOGY 系统接口
        String data = HttpRequest.post(FcHttpCon.ZH_YFSY_XQ).body(json_param).execute().body();

        String result="ERROR";

        try {
            Object succesResponse = JSON.parse(data);
            Map map = (Map) succesResponse;
            Map map1 = (Map) map.get("payload");
            Map map2 = (Map) map1.get("std_data");
            Map map3 = (Map) map2.get("execution");
            Map map4 = (Map) map2.get("parameter");
            System.out.println(map4.toString());

            if ("0".equals(map3.get("code"))) {
                result = "SUCCESS";
            }

            JSONArray jsonArray = new JSONArray(map4.get("wo_info"));
            for( int i=0;i<jsonArray.size();i++){
                JSONObject info = new JSONObject((Map<String, Object>) jsonArray.get(i));
                Map mapg = (Map) info;
                System.out.println(mapg.get("execute_description").toString());
            }
            } catch(Exception e){
            }


            System.out.println("RESULT:" + result);


    }
}
